<?php
namespace Orm\Article;

use Base\Exception\RmException;
use Think\Exception;
use V1\ArticleContent;

class ArticleClass extends \Base\BaseOrmObject{
    protected   $id,
				$uid = 0, // 用户ID
				$title = '', // 文章标题
				$remark = '', // 备注（描述）
				$img = '',
                $reading_times = 0, //阅读次数
				$status = '', //状态，0，预生成1，未发布2，已发布3，下架
				$is_del = 0, // 默认0  是否删除 1是 0否
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    //状态，0，未发布1，已发布2，下架
    const STATUS_UNPUBLISHED  = 0;
    const STATUS_ISSUED  = 1;
    const STATUS_OFFTHESHELF = 2;

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
        $this->img = $this->getImg();
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Article\ArticleModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'uid' => $this->uid,
			'title' => $this->title,
			'reading_times' => $this->reading_times,
			'remark' => $this->remark,
			'status' => self::STATUS_UNPUBLISHED,
        );

        $model = new \Orm\Article\ArticleModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'uid' => $this->uid,
			'title' => $this->title,
            'reading_times' => $this->reading_times,
			'remark' => $this->remark,
			'status' => $this->status,
        );

        $model = new \Orm\Article\ArticleModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Article\ArticleModel();
        $model->update($data, "`id` = {$id}");
    }

    public function completeContent()
    {
        $this->conent_list = array();
        $article_content_list = new \Orm\ArticleContent\ArticleContentList();
        $article_content_list['where'] = " article_id = {$this->id} ";
        $article_content_list['limit'] = \Util\Config::get('max_limit');
        try {
            $article_content_list->load();
            foreach ($article_content_list['lists'] as $item) {
                $content = array(
                    'id' => $item['id'],
                    'article_id' => $item['article_id'],
                    'type' => $item['type'],
                    'content' => $item['content'],
                    'sort' => $item['sort'],
                    'insert_time' => $item['insert_time'],
                    'update_time' => $item['update_time'],
                );
                //  如果为商品类型的话补全商品信息
                if ($item['type'] == \Orm\ArticleContent\ArticleContentClass::TYPE_PRODUCT) {
                    try {
                        $product_class = new \Orm\Product\ProductClass();
                        $product_class->loadValidById($item['content']);
                        $product_class->completeImageList($product_class['id']);
                        $content['product_info'] = $product_class->objToArray($product_class);
                    }catch (\Exception $e){}
                }
                $this->conent_list[] = $content;
            }
        }catch (\Exception $e) {}
    }

    private function getImg()
    {
        $content_cls = new \Orm\ArticleContent\ArticleContentClass();
        return $content_cls->loadArticleImg($this->id);
    }
    private function checkNecessaryParam()
    {
        if (intval($this->uid) == 0) throw new Exception('对不起，创建人不能为空');
    }
}